package {

	/**
	 * @author Ritm
	 */
		
	
	import mx.collections.ArrayCollection;	  
	import mx.controls.Alert;
	
	import mx.core.FlexGlobals;

	import flash.filesystem.File;

	import my1flex;
	import Lang;
		  
	public class commonFun {
		
		
				
		/*
		 * 
		 * res = {type, mes}
		 * 
		 */		
		public static function strerror(ob, la:Lang){
			
			var res = '';
			
			switch (ob.type){
				
				case  0x81 : res = la.ERR_COMM_NOT_FOUND;
							 break; 
				case  0x82 : res = la.ERR_STRUCT_EXIST;
							 break; 
				case  0x83 : res = la.ERR_QUANT_PARAM;
							 break; 
				case  0x84 : res = la.ERR_TYPE;
							 break; 
				case  0x85 : res = la.ERR_SIZE_PARAM;
							 break; 
				case  0x86 : res = la.ERR_RO;
							 break; 
				case  0x87 : res = la.ERR_ROM;
							 break;
				case  0x88 : res = la.ERR_CRC;
							 break; 			  			 			 			 			 
				//case  0x89 : res = la.ERR_COMMAND_FORMAT;
				//			 break; 
			}
			
			return res  + (ob.mes ? (' ' + ob.mes) : '');
					
		}
		
		public static function prepObTo(index , nstruct, mdata){
				var res = {};
				//res[index] = {nstruct : {}};
				if (!(mdata is Array)) mdata = [mdata];
				res[index] = {};
				res[index][nstruct] = {};
				for (var i in mdata) res[index][nstruct][i] = mdata[i];
				return res;
		}
		
		public static function getParts(bitParts, la:Lang){
				
				var res:ArrayCollection = new ArrayCollection();
				res.addItem({label : la.no, data : 'no',  ch_ : bitParts == 0});
				 for (var i=0; i<8; i++)
						res.addItem({label : la.part + ' ' + (i), data : i,  ch_ : bitParts & (1 << i)});
						
				return res;
			}
			
		public static function getPartsN(mpart, la:Lang, count=100){
				
				var res:ArrayCollection = new ArrayCollection();
				 for (var i=1; i<count; i++){
					res.addItem({label : la.part + ' ' + (i), data : i,  ch_ : my1flex.inArr(mpart, i)});
				 }
				var one;
				for (var n in mpart)
					if (mpart[n]) one = true;
				res.addItemAt({label : la.no, data : 'no',  ch_ : !one}, 0);
				return res;
			}	


		public static function getZones(bitParts, la:Lang){
				
				var res:ArrayCollection = new ArrayCollection();
				res.addItem({label : la.no, data : 'no',  ch_ : bitParts == 0});
				 for (var i=0; i<100; i++)
						res.addItem({label : la.zone + ' ' + (i), data : i,  ch_ : bitParts & (1 << 1)});
						
				return res;
			}
									
		public static function getPartsBit(ar:ArrayCollection){
				
				var res = 0;
				 for (var i=1; i<ar.length; i++)
				 		if (ar.getItemAt(i).ch_)  res ^= (1 << (i-1));
						
				return res;
			}	
			
			
		 public static function isSomeCanged(ob){
		 	
				 for (var i in ob) 
				 	if (ob[i]) return true;
				return false;
			}	
			
		  public static function setAllCanged(ob, ch=false, prop=''){
		 	
				 for (var i in ob) prop ? ob[i][prop] = false : ob[i] = false; 
			}		
	
	
	
		  public static function getLStateOutsAndSh(byte, n, la:Lang){
		 	
				var m = n ? la.rele_list_sostOut : la.rele_list_sostSh;
				//Alert.show(m + '___' + (byte & (1 << n)));
				return m[byte & (1 << n) ? 1 : 0];
			}		
	
		
			
		public static function copySchemeAndData(){
				
				
		 		var f:File = File.userDirectory.resolvePath("Local Settings/Application Data/cont/data");
		 		var fscheme:File = File.applicationDirectory.resolvePath("resource/scheme.xml");
		 		var fdata:File = File.applicationDirectory.resolvePath("resource/data.xml");
		 		
		 		
		 		//if(!(fscheme.exists || fdata.exists)) return false; 
		 		//var s = my1flex.dirname(f.nativePath);
		 		//f = new File(s).resolvePath("data");
		 		
		 	  try{
		 			if (!f.exists) f.createDirectory();
		 			
		 			if (fscheme.exists || fdata.exists)
		 			
		 					Alert.show(f.nativePath + ' уже есть файлы, заменить схему и данные? ', 'conf', Alert.YES|Alert.NO, null, function(e){
		 						
		 							if (e.detail != Alert.YES) return;
		 						
						 			if (fscheme.exists){ 
					 					fscheme.copyTo(f.resolvePath(fscheme.name), true);
										//fscheme.deleteFile();
						 			}
						 			if (fdata.exists){
										fdata.copyTo(f.resolvePath(fdata.name), true);
										//fdata.deleteFile();
						 			}
		 					});
		 					
						}catch(e){
						 			Alert.show('file: ' + e);
						 		}
		 						
		 		
			}
			
			
		  public static function setAppearance(el){
		  		
				var vis;
				var inf = FlexGlobals.topLevelApplication.loc.devInf;
				var els = FlexGlobals.topLevelApplication.elForAppearance;
				var mver = inf.mver;
				
				switch(el.id){
			  			
			  			case   'cmasterPart'		:	el.includeInLayout = el.visible = mver[0] == 16;		
			  										break;
			  			
			  		}
			  	if (!my1flex.inArr(els, el)) els.push(el);	
	 			
	 	  }		
	
			
			
			
			
			
		}		
		
}
